import chartTool from "../../tools/chartTool.js";
import slashTool from "../../tools/slashTool.js";
import $ from "jquery";
const server = "https://auod-beijing.oss-cn-beijing.aliyuncs.com";

export function afterRenderer (td, row, col, prop, value, cellProperties) {
  if (!this.context) {
    return;
  }
  const cellDef = this.context.getCell(row, col);
  if (!cellDef) {
    return;
  }

  const cellStyle = cellDef.cellStyle;
  const cellValue = cellDef.value;
  const $td = $(td);
  const valueType = cellValue.type;
  let tip = "";
  if (valueType === "dataset") {
    if (cellValue.datasetName) {
      tip = cellValue.datasetName + "." + cellValue.aggregate + "(";
      tip += (cellValue.property || "") + ")";
    }
    $td.html(tip);
  } else if (valueType === "expression") {
    tip = cellValue.value || "";
    if ($td.html() === "") {
      $td.html(tip);
    }
  } else if (valueType === "image") {
    tip = `${window.i18n.table.render.image}` + cellValue.value;
    const imagePath = server + "/ureport/icon/image.svg";
    const image = $(`<img src="${imagePath}" width="20px">`);
    $td.empty();
    $td.append(image);
  } else if (valueType === "slash") {
    // tip = `${window.i18n.table.render.slash}`;
    // if (!cellDef.crossTabWidget) {
    //   cellDef.crossTabWidget = new CrossTabWidget(this.context, row, col, cellDef);
    // }
    // cellDef.crossTabWidget.doDraw(cellDef, row, col);
    slashTool.doDraw(this.context, row, col);
  } else if (valueType === "zxing") {
    let imagePath = server + "/ureport/icon/qrcode.svg";
    tip = `${window.i18n.table.render.qrcode}`;
    if (cellValue.category === "barcode") {
      tip = `${window.i18n.table.render.barcode}`;
      imagePath = server + "/ureport/icon/barcode.svg";
    }
    const width = cellValue.width;
    const height = cellValue.height;
    const image = $(`<img src="${imagePath}" width="${width}px" height="${height}px">`);
    $td.empty();
    $td.append(image);
  } else if (valueType === "chart") {
    tip = `${window.i18n.table.render.chart}`;
    chartTool.renderChart($td, this.context, row, col);
  } else {
    tip = cellValue.value || "";
    if ($td.html() === "") {
      $td.html(tip);
    }
  }
  $td.prop("title", tip);
  if (valueType === "simple") {
    let text = $td.text();
    if (text && text !== "") {
      text = text.replace(new RegExp(/</gi, "gm"), "&lt;");
      text = text.replace(new RegExp(/>/gi, "gm"), "&gt;");
      text = text.replace(new RegExp(/\r\n/gi, "gm"), "<br>");
      text = text.replace(new RegExp(/\n/gi, "gm"), "<br>");
      text = text.replace(new RegExp(/ /gi, "gm"), "&nbsp;");
      $td.html(text);
    }
  }
  let style = "word-break: break-all;line-height:normal;white-space:nowrap;padding:0 1px;";
  // 增加自定义样式
  if (cellStyle.customStyles) {
    style += cellStyle.customStyles;
  }
  $td.attr("style", style);

  if (cellDef.expand === "Down") {
    let url = server + "/ureport/icon/expr-expand-down.svg";
    if (valueType === "dataset") {
      url = server + "/ureport/icon/expand-down.svg";
    }
    $td.prepend(`<image src="${url}"></image>`);
  } else if (cellDef.expand === "Right") {
    let url = server + "/ureport/icon/expr-expand-right.svg";
    if (valueType === "dataset") {
      url = server + "/ureport/icon/expand-right.svg";
    }
    $td.prepend(`<image src="${url}" style="display: block;"></image>`);
  } else {
    if (valueType === "dataset") {
      const url = server + "/ureport/icon/property.svg";
      $td.prepend(`<image src="${url}" style="display: inline-block;"></image>`);
    } else if (valueType === "expression") {
      const url = server + "/ureport/icon/expression.svg";
      $td.prepend(`<image src="${url}" style="display: inline-block;"></image>`);
    }
  }
  if (cellStyle.align) {
    $td.css("text-align", cellStyle.align);
  }
  if (cellStyle.valign) {
    $td.css("vertical-align", cellStyle.valign);
  }
  if (cellStyle.bold) {
    $td.css("font-weight", "bold");
  }
  if (cellStyle.italic) {
    $td.css("font-style", "italic");
  }
  if (cellStyle.underline) {
    $td.css("text-decoration", "underline");
  }
  if (cellStyle.forecolor) {
    let forecolor = cellStyle.forecolor;
    if (forecolor.indexOf("rgb") < 0) {
      forecolor = "rgb(" + forecolor + ")";
    }
    $td.css("color", forecolor);
  }
  if (cellStyle.bgcolor) {
    let bgcolor = cellStyle.bgcolor;
    if (bgcolor.indexOf("rgb") < 0) {
      bgcolor = "rgb(" + bgcolor + ")";
    }
    $td.css("background-color", bgcolor);
  }
  if (cellStyle.fontSize) {
    $td.css("font-size", cellStyle.fontSize + "pt");
  }
  if (cellStyle.fontFamily) {
    $td.css("font-family", cellStyle.fontFamily);
  }
  if (cellStyle.lineHeight) {
    $td.css("line-height", cellStyle.lineHeight);
  } else {
    $td.css("line-height", "");
  }

  const leftBorder = cellStyle.leftBorder;
  if (leftBorder) {
    if (leftBorder === "" || leftBorder.style === "none") {
      $td.css({
        "border-left": ""
      });
    } else {
      let borderStyle = "double";
      let borderWidth = leftBorder.width;
      if (borderWidth === null || borderWidth === undefined || borderWidth === "") {
        borderWidth = 0;
      } else {
        borderWidth = parseInt(borderWidth);
      }
      if (leftBorder.style !== "solid" && borderWidth > 0) {
        borderStyle = leftBorder.style;
        borderWidth++;
      }
      const color = leftBorder.color.indexOf("rgb") >= 0 ? leftBorder.color : "rgb(" + leftBorder.color + ")";
      const style = borderStyle + " " + borderWidth + "px " + color;
      $td.css({
        "border-left": style
      });
    }
  }

  const rightBorder = cellStyle.rightBorder;
  if (rightBorder) {
    if (rightBorder === "" || rightBorder.style === "none") {
      $td.css({
        "border-right": ""
      });
    } else {
      let borderStyle = "double";
      let borderWidth = rightBorder.width;
      if (borderWidth === null || borderWidth === undefined || borderWidth === "") {
        borderWidth = 0;
      } else {
        borderWidth = parseInt(borderWidth);
      }
      if (rightBorder.style !== "solid" && borderWidth > 0) {
        borderStyle = rightBorder.style;
        borderWidth++;
      }
      const color = rightBorder.color.indexOf("rgb") >= 0 ? rightBorder.color : "rgb(" + rightBorder.color + ")";
      const style = borderStyle + " " + borderWidth + "px " + color;
      $td.css({
        "border-right": style
      });
    }
  }
  const topBorder = cellStyle.topBorder;
  if (topBorder) {
    if (topBorder === "" || topBorder.style === "none") {
      $td.css({
        "border-top": ""
      });
    } else {
      let borderStyle = "double";
      let borderWidth = topBorder.width;
      if (borderWidth === null || borderWidth === undefined || borderWidth === "") {
        borderWidth = 0;
      } else {
        borderWidth = parseInt(borderWidth);
      }
      if (topBorder.style !== "solid" && borderWidth > 0) {
        borderStyle = topBorder.style;
        borderWidth++;
      }
      const color = topBorder.color.indexOf("rgb") >= 0 ? topBorder.color : "rgb(" + topBorder.color + ")";
      const style = borderStyle + " " + borderWidth + "px " + color;
      $td.css({
        "border-top": style
      });
    }
  }
  const bottomBorder = cellStyle.bottomBorder;
  if (bottomBorder) {
    if (bottomBorder === "" || bottomBorder.style === "none") {
      $td.css({
        "border-bottom": ""
      });
    } else {
      let borderStyle = "double";
      let borderWidth = bottomBorder.width;
      if (borderWidth === null || borderWidth === undefined || borderWidth === "") {
        borderWidth = 0;
      } else {
        borderWidth = parseInt(borderWidth);
      }
      if (bottomBorder.style !== "solid" && borderWidth > 0) {
        borderStyle = bottomBorder.style;
        borderWidth++;
      }
      const color = bottomBorder.color.indexOf("rgb") >= 0 ? bottomBorder.color : "rgb(" + bottomBorder.color + ")";
      const style = borderStyle + " " + borderWidth + "px " + color;
      $td.css({
        "border-bottom": style
      });
    }
  }
}
